package net.wanho.zaixian.course.controller;

import com.mongodb.client.gridfs.GridFSBucket;
import net.wanho.zaixian.course.service.CmsPageService;
import net.wanho.zaixian.pojo.cms.CmsPage;
import org.bson.types.ObjectId;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.*;

@Controller
public class HttpFetchController {

    @Resource
    private GridFSBucket gridFSBucket;

    @Resource
    private CmsPageService cmsPageService;


    //接收queue类型消息
    //destination对应配置类中ActiveMQQueue("springboot.queue")设置的名字
    @JmsListener(destination="springboot.queue")
    public void ListenQueue(String msg){
        System.out.println("接收到queue消息：" + msg);
    }

    //接收topic类型消息
    //destination对应配置类中ActiveMQTopic("springboot.topic")设置的名字
    //containerFactory对应配置类中注册JmsListenerContainerFactory的bean名称
    @JmsListener(destination="springboot.topic", containerFactory = "jmsTopicListenerContainerFactory")
    public void ListenTopic(String cmsPageId) throws FileNotFoundException {
        System.out.println("接收到topic消息：" + cmsPageId);
        CmsPage cmsPage = cmsPageService.getPage(cmsPageId).get();
        String htmlFileId = cmsPage.getHtmlFileId();
        File file = new File("D:\\1001.html");
        OutputStream os=new FileOutputStream(file);
        gridFSBucket.downloadToStream(new ObjectId(htmlFileId),os);


    }



}
